package ext.acae2.change.ui;

import java.util.Vector;

import org.apache.log4j.Logger;

import wt.doc.WTDocument;
import wt.enterprise.RevisionControlled;
import wt.fc.Persistable;
import wt.lifecycle.LifeCycleManaged;
import wt.lifecycle.State;
import wt.log4j.LogR;
import wt.org.WTGroup;
import wt.org.WTPrincipal;
import wt.util.WTException;

import com.ptc.core.ui.validation.DefaultSimpleValidationFilter;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationStatus;

import ext.acae2.ACAEConstants;
import ext.acae2.utilities.principal.CSCPrincipal;

/**
 * @desription 文件作废通告的
 * @author cnie
 * @date 2013-6-14 上午11:06:53
 */
public class FileObsoletCreateFilter extends DefaultSimpleValidationFilter{
	private static final Logger LOGGER = LogR.getLogger(FileObsoletCreateFilter.class
			.getName());
	@Override
	public UIValidationStatus preValidateAction(
			UIValidationKey uivalidationkey,
			UIValidationCriteria uivalidationcriteria) {
		UIValidationStatus status = UIValidationStatus.HIDDEN;  // HIDDEN
		try {
			WTPrincipal currentUser = uivalidationcriteria.getUser()
					.getPrincipal();

			Persistable p = uivalidationcriteria.getContextObject()
					.getObject();
			
			WTGroup adminGroup = CSCPrincipal.getGroupByName("Administrators");
			Vector adminUsers = new Vector();
			adminUsers = CSCPrincipal.getGroupMemberUsers(adminGroup, adminUsers);
			
			LOGGER.debug("CNCreateFilter contextObject============>"+p.getClass().getName());
			if (p instanceof RevisionControlled) {

				RevisionControlled rc = (RevisionControlled) p;
				if(rc instanceof WTDocument){
					WTPrincipal creator = rc.getCreator().getPrincipal();
					WTPrincipal modifier = rc.getModifier().getPrincipal();
					if (creator.equals(currentUser)||modifier.equals(currentUser)||adminUsers.contains(currentUser)) {
						if (rc instanceof LifeCycleManaged) {
							LifeCycleManaged lm = (LifeCycleManaged) p;
							if (State.toState(
									ACAEConstants.ACAE2_RELEASED_STATE_NAME).equals(
									lm.getState().getState())) {
								status = UIValidationStatus.ENABLED;
							}
						}
					} 
				}
			}
		} catch (WTException e) {
			e.printStackTrace();
		}

		return status;
		
	}

}
